<?php
namespace Base\Controller;

use Admin\Model\DoctorInfoModel;
use Admin\Model\DoctorPatientRelativeModel;
use Admin\Model\FriendInfoModel;
use Admin\Model\PatientInfoModel;
use Admin\Model\PatientMatchInfoModel;
use Admin\Model\PatientOperationInfoModel;
use Admin\Model\PatientPrimarydiseaseInfo;
use Admin\Model\PatientPrimarydiseaseInfoModel;
use Admin\Model\UserModel;
use Think\Controller;

class BaseHomeController extends Controller
{
    public $user;
    protected $relation;//关联
    protected $upload;//是否有上传字段
    protected $uploadConfig;//上传参数配置
    public function __construct($relation = true, $upload = false, $uploadConfig = array(
        'input_name' => 'coverFile',//文本框name
        'save_path' => 'cover',//保存路径
        'data_field' => 'cover'//数据库字段
    ))
    {
        parent::__construct();
        $this->upload = $upload;
        $this->uploadConfig = $uploadConfig;
        if (isMobile()) {
            C('DEFAULT_THEME', 'Default/Html/Mobile');
        }
        $columns =array(
            array(  'id'=>1,'title'=>'待诊疗'),
            array(  'id'=>4,'title'=>'特别关注'),
            array(  'id'=>2,'title'=>'我的病人'),
        );
        $this->assign('column',$columns);
    }
    /**
     * 初始化模型
     */
    private function initModel()
    {
        if ($this->relation) {
            $this->model = D($this->table)->relation($this->relation);
        } else {
            $this->model = M($this->table);
        }
    }
    /**
     * 是否登录
     * */
    public function isLogin()
    {
        return session('userId') ? session('userId') : false;
    }

    /**
     * 退出登录
     */
    public  function loginout(){
         session(null);
        $this->redirect('Index/index');
    }
  public  function dathregist(){
      $PatientInfoModel=new  PatientInfoModel();
      $PatientOperationInfoModel = new  PatientOperationInfoModel();
      $DoctorPatientRelativeModel=new DoctorPatientRelativeModel();
      $info['realname']=I('realname');
      $info['phone']=time().'1';
      $info['bloodid']=I('bloodid');
      $info['address']=I('address');
      $info['birthday']=I('birthday');
      $info['sex']=I('sex');
      $info['status']=1;
      $info['createtime']=date("Y-m-d H:i:s");
      $info['lastedittime']=date("Y-m-d H:i:s");
      $id=$PatientInfoModel->add($info);
      $Operation['patientid']=$id;
      $Operation['doctorid']=session('userId');
      $Operation['operationtime']=I('operationtime');
      $Operation['sourceid']=I('sourceid');
      $Operation['hospitalid']=I('hospitalid');
      $Operation['commonid']=I('commonid');
      $Operation['logtime']=date("Y-m-d H:i:s");
      $optiondata=$PatientOperationInfoModel->add($Operation);
      $Relative['patientid']=$id;
      $Relative['doctorid']=session('userId');
      $Relative['relationtype']=2;
      $Relative['operationtime']=date("Y-m-d H:i:s");
      $Relativedata=$DoctorPatientRelativeModel->add($Relative);
      if($optiondata && $Relativedata){
         return true;
      }else{
          return false;
      }

  }
    /**
     * 给死亡患者添加新的信息
     */
    public  function deathpatient(){
          $result=$this->dathregist();
            if($result){
            $data['status']=1;
            $data['msg']='患者信息提交成功！';
             }else{
            $data['status']=0;
            $data['msg']='患者信息提交失败，请检查信息是否齐全！';
            }
           echo json_encode($data);
     }
    /**
     *给患者配型
     */
    public  function patientmatching(){
        $PatientMatchInfoModel=new PatientMatchInfoModel();
        $FriendInfoModel =new FriendInfoModel();
        $PatientPrimarydiseaseInfo=new PatientPrimarydiseaseInfoModel();
        $where['userid']=I('patientid');
        $where['userfellowid']=I('commonid');
        $res=$FriendInfoModel->where($where)->find();
        $disease['patientid']=I('patientid');
        $diseaseInfo=$PatientPrimarydiseaseInfo->where($disease)->find();
         if( !$diseaseInfo){
             $disease['primarydiseaseid']=I('primarydiseaseid');
             $disease['primarydiseasename']=getfildname('diseaseinfo',I('primarydiseaseid'));
             $disease['diagnosetime']=date("Y-m-d H:i:s");
             $PatientPrimarydiseaseInfo->add($disease);
          }
          if(!$res){
              $where['status']=3;
              $where['usertype']=0;
              $where['logtime']=date("Y-m-d H:i:s");
              $FriendInfoModel->add($where);
          }
            $data=$PatientMatchInfoModel->create();
            $data['value']=json_encode($data['value']);
            $results=$PatientMatchInfoModel->add($data);
        if($results){
            $msg['status']=1;
            $msg['msg']='配型信息添加成功！';
        }else{
            $msg['status']=0;
            $msg['msg']='配型信息添加失败，请检查您输入的信息是否完整！';
        }
        echo  json_encode($msg);
    }
}